import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import '../../core/constants.dart';  

/// 图片资源数据模型
/// 
/// 设计思路：
/// 1. 封装图片相关的所有信息
/// 2. 支持分类和标签系统
/// 3. 为未来的在线图片、用户图片等功能预留接口
class AssetImageModel extends Equatable {
  /// 图片唯一标识
  final String id;
  
  /// 图片路径
  final String path;
  
  /// 图片显示名称
  final String name;
  
  /// 图片分类
  final String category;
  
  /// 图片描述
  final String description;
  
  /// 图片标签
  final List<String> tags;
  
  /// 难度建议（某些图片可能更适合特定难度）
  final DifficultyLevel? recommendedDifficulty;
  
  /// 图片尺寸信息（可选，用于优化）
  final Size? originalSize;
  
  /// 是否为用户自定义图片
  final bool isUserGenerated;
  
  /// 创建时间
  final DateTime createdAt;
  
  const AssetImageModel({
    required this.id,
    required this.path,
    required this.name,
    required this.category,
    this.description = '',
    this.tags = const [], // 修复const问题
    this.recommendedDifficulty,
    this.originalSize,
    this.isUserGenerated = false,
    required this.createdAt,
  });
  
  /// 创建内置资源图片
  factory AssetImageModel.asset({
    required String id,
    required String path,
    required String name,
    required String category,
    String description = '',
    List<String> tags = const [],
    DifficultyLevel? recommendedDifficulty,
  }) {
    return AssetImageModel(
      id: id,
      path: path,
      name: name,
      category: category,
      description: description,
      tags: tags,
      recommendedDifficulty: recommendedDifficulty,
      isUserGenerated: false,
      createdAt: DateTime.now(),
    );
  }
  
  /// 创建用户图片
  factory AssetImageModel.userImage({
    required String id,
    required String path,
    String name = '自定义图片',
    String description = '',
    List<String> tags = const [],
  }) {
    return AssetImageModel(
      id: id,
      path: path,
      name: name,
      category: '用户图片',
      description: description,
      tags: tags,
      isUserGenerated: true,
      createdAt: DateTime.now(),
    );
  }
  
  /// 获取Flutter ImageProvider
  ImageProvider get imageProvider {
    if (isUserGenerated || path.startsWith('http')) {
      // 用户图片或网络图片
      return NetworkImage(path);
    } else {
      // 资源图片
      return AssetImage(path);
    }
  }
  
  /// 获取图片预览用的AssetImage
  AssetImage get assetImage => AssetImage(path);
  
  /// 是否匹配搜索关键词
  bool matchesKeyword(String keyword) {
    final lowerKeyword = keyword.toLowerCase();
    return name.toLowerCase().contains(lowerKeyword) ||
           description.toLowerCase().contains(lowerKeyword) ||
           category.toLowerCase().contains(lowerKeyword) ||
           tags.any((tag) => tag.toLowerCase().contains(lowerKeyword));
  }
  
  /// 创建副本
  AssetImageModel copyWith({
    String? id,
    String? path,
    String? name,
    String? category,
    String? description,
    List<String>? tags,
    DifficultyLevel? recommendedDifficulty,
    Size? originalSize,
    bool? isUserGenerated,
    DateTime? createdAt,
  }) {
    return AssetImageModel(
      id: id ?? this.id,
      path: path ?? this.path,
      name: name ?? this.name,
      category: category ?? this.category,
      description: description ?? this.description,
      tags: tags ?? this.tags,
      recommendedDifficulty: recommendedDifficulty ?? this.recommendedDifficulty,
      originalSize: originalSize ?? this.originalSize,
      isUserGenerated: isUserGenerated ?? this.isUserGenerated,
      createdAt: createdAt ?? this.createdAt,
    );
  }
  
  @override
  List<Object?> get props => [
    id,
    path,
    name,
    category,
    tags,
    isUserGenerated,
  ];
  
  @override
  String toString() => 'AssetImageModel('
      'id: $id, '
      'name: $name, '
      'category: $category, '
      'isUserGenerated: $isUserGenerated)';
}

/// 图片分类枚举
enum ImageCategory {
  nature('自然风光', Icons.landscape, Color(0xFF4CAF50)),
  animals('动物世界', Icons.pets, Color(0xFFFF9800)),
  architecture('建筑艺术', Icons.location_city, Color(0xFF2196F3)),
  userImages('我的图片', Icons.photo_library, Color(0xFF9C27B0)),
  all('全部', Icons.apps, Color(0xFF607D8B));
  
  const ImageCategory(this.displayName, this.icon, this.color);
  
  final String displayName;
  final IconData icon;
  final Color color;
}

/// 图片集合模型
class ImageCollection extends Equatable {
  final String id;
  final String name;
  final List<AssetImageModel> images;
  final ImageCategory category;
  final DateTime createdAt;
  final DateTime updatedAt;
  
  const ImageCollection({
    required this.id,
    required this.name,
    required this.images,
    required this.category,
    required this.createdAt,
    required this.updatedAt,
  });
  
  /// 创建空集合
  factory ImageCollection.empty(String name, ImageCategory category) {
    final now = DateTime.now();
    return ImageCollection(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      name: name,
      images: const [], // 修复const问题
      category: category,
      createdAt: now,
      updatedAt: now,
    );
  }
  
  /// 图片数量
  int get imageCount => images.length;
  
  /// 是否为空
  bool get isEmpty => images.isEmpty;
  
  /// 添加图片
  ImageCollection addImage(AssetImageModel image) {
    return copyWith(
      images: [...images, image],
      updatedAt: DateTime.now(),
    );
  }
  
  /// 移除图片
  ImageCollection removeImage(String imageId) {
    return copyWith(
      images: images.where((img) => img.id != imageId).toList(),
      updatedAt: DateTime.now(),
    );
  }
  
  /// 根据关键词搜索图片
  List<AssetImageModel> searchImages(String keyword) {
    if (keyword.isEmpty) return images;
    return images.where((img) => img.matchesKeyword(keyword)).toList();
  }
  
  /// 创建副本
  ImageCollection copyWith({
    String? id,
    String? name,
    List<AssetImageModel>? images,
    ImageCategory? category,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return ImageCollection(
      id: id ?? this.id,
      name: name ?? this.name,
      images: images ?? this.images,
      category: category ?? this.category,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }
  
  @override
  List<Object?> get props => [id, name, images, category, updatedAt];
}
